package com.hrzyit.whistle.comm.ui.activity

import android.graphics.Color
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.hrzyit.base.ui.activity.BaseMvpActivity
import com.hrzyit.provider.routers.RouterPath
import com.hrzyit.whistle.comm.R
import com.hrzyit.whistle.comm.data.bean.DepartmentEntity
import com.hrzyit.whistle.comm.presenter.DepartmentPresenter
import com.hrzyit.whistle.comm.presenter.view.DepartmentView
import com.hrzyit.whistle.comm.ui.adapter.DepartmentAdapter
import kotlinx.android.synthetic.main.activity_department.*
import me.yokeyword.indexablerv.IndexableLayout
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast
import java.util.*

/**
 * 单位选择界面
 */
@Route(path = RouterPath.CommunicationCenter.PATH_COMM_DEPARTMENT)
class DepartmentActivity : BaseMvpActivity<DepartmentPresenter>(), DepartmentView {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_department)

        initView()

        initAdapter()
    }

    private fun initView() {

    }
    override fun injectComponent() {

    }
    lateinit var mAdapter:DepartmentAdapter
    private fun initAdapter() {
        // 初始化单位索引选择器
        // 创建联系人适配器
        var _indexableLayout = departmentIndexLayout as IndexableLayout
        _indexableLayout.setLayoutManager(LinearLayoutManager(this))

        // setAdapter
         mAdapter = DepartmentAdapter(this)
        _indexableLayout.setAdapter(mAdapter)
        // set Datas

        // set Material Design OverlayView
        _indexableLayout.setOverlayStyle_MaterialDesign(Color.BLUE)

        // 全字母排序。  排序规则设置为：每个字母都会进行比较排序；速度较慢
        _indexableLayout.setCompareMode(IndexableLayout.MODE_FAST)

        // set Listener
        mAdapter.setOnItemContentClickListener { _, originalPosition, currentPosition, entity ->
            if (originalPosition >= 0) {
                toast("选中: ${entity.simpleName} 当前位置:" + currentPosition + "  原始所在数组位置:" + originalPosition)
                startActivity<ContactActivity>()
            } else {
                toast("选中Header/Footer: ${entity.simpleName}  当前位置:" + currentPosition)
            }
        }
        mAdapter.setOnItemTitleClickListener { _, currentPosition, indexTitle ->
            toast("选中:$indexTitle  当前位置:$currentPosition")
        }
    }

//    private fun initDataList(): ArrayList<DepartmentEntity> {
//        val list = ArrayList<DepartmentEntity>()
//        // 初始化数据
//        // TODO 从数据库中加载数据源
//        val contactStrings = Arrays.asList(*resources.getStringArray(R.array.department_array))
//        var departmentEntity: DepartmentEntity?
//        for (i in contactStrings.indices) {
//            departmentEntity = DepartmentEntity()
//            departmentEntity.name = contactStrings[i]
//            list.add(departmentEntity)
//        }
//        return list
//    }


    override fun onGetSelectContactDeptListSuccess(result: MutableList<DepartmentEntity> ) {
        mAdapter.setDatas(result)
    }
}
